﻿/*
作者：张雁鸿
时间：2010-5-23
版本：1.0

Author: Zhang Yanhong
Date: 2010-5-23
Version: 1.0

Depend on jQuery library 1.4.2

Sample:

//direct use mousewheel method
$(".title").mousewheel(
    funcion(event,delta){
        alert(delta);
    }
);

//Use jQuery's bind method to hoot event.
$(".title").bind("mousewheel",function(event,delta){alert(delta);});

$(".title").unbind("mousewheel");

*/
(function($) {
    $.fn.extend({
        mousewheel: function(handler) {
            this.bind($.event.Mousewheel.eventName, handler);
        }
    }
    );
    $.event.Mousewheel = {
        eventName: "mousewheel",
        start: function() {
            if (this._initialized) return;
            var wheel = $.event.Mousewheel.handler;
            /** Initialization code. 
            * If you use your own event management code, change it as required.
            */
            if (window.addEventListener)
            /** DOMMouseScroll is for mozilla. */
                window.addEventListener('DOMMouseScroll', wheel, false);
            /** IE/Opera. */
            window.onmousewheel = document.onmousewheel = wheel;
            this._initialized = true;
        },
        stop: function() {
            if (!this._initialized) return;
            var wheel = $.event.Mousewheel.handler;
            if (window.removeEventListener)
                window.removeEventListener('DOMMouseScroll', wheel, false);
            window.onmousewheel = document.onmousewheel = null;
            this._initialized = false;
        },
        /** Event handler for mouse wheel event.*/
        handler: function(event) {
            var delta = 0;
            if (!event) /* For IE. */
                event = window.event;
            if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta / 120;
                /** In Opera 9, delta differs in sign as compared to IE.
                */
                if (window.opera)
                    delta = -delta;
            } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                * Also, delta is multiple of 3.
                */
                delta = -event.detail / 3;
            }
            if (!$.event.global[$.event.Mousewheel.eventName]) return;
            /** If delta is nonzero, handle it.
            * Basically, delta is now positive if wheel was scrolled up,
            * and negative, if wheel was scrolled down.
            */
            if (delta) { //handle(event, delta);
                var target = event.target;
                if (!event.target) {
                    target = event.srcElement || document;
                }
                var e = $.Event(event);
                e.delta = delta;
                e.type = $.event.Mousewheel.eventName;
                $(target).trigger(e, [delta]);
            }
        }
    };
    $.event.Mousewheel.start(); //initialized
})(jQuery);
